package com.hongfeng.web.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.hongfeng.web.commons.Page;
import com.hongfeng.web.controller.common.BaseController;
import com.hongfeng.web.model.AutoType;
import com.hongfeng.web.model.Product;
import com.hongfeng.web.service.AutoTypeService;
import com.hongfeng.web.service.ProductService;

@Controller
public class ProductController extends BaseController {

	@Resource
	private AutoTypeService autoTypeService;
	@Resource
	private ProductService productService;

	@RequestMapping("list")
	public String list(@RequestParam(required = false) Long brandId,
			@RequestParam(required = false) Long typeId,
			@RequestParam(required = false) Integer pageIndex, Model model)
			throws Exception {
		// 车型分类
		List<AutoType> autoTypes = null;
		if (brandId == null) {
			autoTypes = autoTypeService.findByBrandName("大众");
		} else {
			autoTypes = autoTypeService.findByBrandId(brandId);
		}

		// 配件
		Page<Product> page = null;
		if (brandId != null) {
			page = productService.findPageByBrandId(pageIndex, brandId);
			model.addAttribute("brandId", brandId);
		} else if (typeId != null) {
			page = productService.findPageByTypeId(pageIndex, typeId);
			model.addAttribute("typeId", typeId);
		} else {
			page = productService.findPage(pageIndex);
		}

		model.addAttribute("autoTypes", autoTypes);
		model.addAttribute("products", page);

		return "products";
	}
}
